package com.biostime.dingdingbox.modules.db.api;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.biostime.dingdingbox.common.api.FastResponse;
import com.biostime.dingdingbox.common.api.ObjResponse;
import com.biostime.dingdingbox.common.web.BaseController;
import com.biostime.dingdingbox.modules.db.entity.VideoRecord;
import com.biostime.dingdingbox.modules.db.service.MemberService;
import com.biostime.dingdingbox.modules.db.service.VideoRecordService;


/**
 * 评论API
 * @author chenweicheng
 * @version 2016-07-01
 */
@RestController
@RequestMapping(value = "${adminPath}/db/api/video")
public class VideoApi extends BaseController {
		
	@Autowired
	private VideoRecordService videoRecordService;
	
	@Autowired
	private MemberService memberService;
	
	
	/**
	 * 播放
	 */
	@RequestMapping(value = "play")
	public ObjResponse<Map<String,Object>> play(String videoId , String userId , HttpServletResponse response) {		
		response.setHeader("Access-Control-Allow-Origin", "*");
		userId = memberService.apiGetUserId(userId);
		Map<String,Object> map = new HashMap<String,Object>();
		String message = "播放成功";				
		int errCode = 0;
		if(userId != null && !userId.equals("") && videoId != null && !videoId.equals("")){
			VideoRecord videoRecord = new VideoRecord();
			videoRecord.setVideoId(Integer.parseInt(videoId));
			videoRecord.setMemberId(Integer.parseInt(userId)); 
			videoRecordService.save(videoRecord); 	
			map.put("videoRecordId",Integer.parseInt(videoRecord.getId()));
		}else{
			message = "播放失败,参数不全";
			errCode = 1;
		}
				
		return  FastResponse.create().errMsg(message).errCode(errCode).obj(map);		
	}
	
	/**
	 * 结束播放
	 */
	@RequestMapping(value = "stop")
	public ObjResponse<Map<String,Object>> stop(String videoRecordId , String startTime , String endTime , String userId,
				HttpServletResponse response) {		
		response.setHeader("Access-Control-Allow-Origin", "*");
		userId = memberService.apiGetUserId(userId);
		Map<String,Object> map = new HashMap<String,Object>();
		String message = "结束播放成功";				
		int errCode = 0;
		if(userId != null && !userId.equals("") && videoRecordId != null && !videoRecordId.equals("")){
			VideoRecord videoRecord = new VideoRecord();
			videoRecord.setId(videoRecordId);
			videoRecord.setStartTime(startTime);
			videoRecord.setEndTime(endTime); 
			videoRecord.setVideoId(Integer.parseInt(videoRecordId));
			videoRecord.setMemberId(Integer.parseInt(userId)); 
			videoRecordService.save(videoRecord); 		
			map.put("videoRecordId",Integer.parseInt(videoRecord.getId()));
		}else{
			message = "结束播放失败,参数不全";
			errCode = 1;
		}
				
		return  FastResponse.create().errMsg(message).errCode(errCode).obj(map);		
	}			
}
